<template>
  <el-card class="app-container">
    <div>
      <div class="itemTitle">{{ title }}</div>
      <div class="search">
        <el-form :inline="true" :model="queryParams" ref="queryForm">
          <el-col :span="5">
            <el-form-item label="隐患名称" prop="name">
              <el-input v-model="queryParams.name" placeholder="请输入隐患名称" clearable />
            </el-form-item>
          </el-col>
          <el-col :span="9">
            <el-form-item>
              <el-button type="primary" @click="handleQuery">搜索</el-button>
              <el-button @click="resetQuery">重置</el-button>
            </el-form-item>
          </el-col>
        </el-form>
      </div>
      <div class="content">
        <div class="ct-table-only">     
          <elTables :table-data="tableData" :columns="tableColumns" :totalLength="total" :currentPage="queryParams.pageNum"
            :handle-current-change="handleCurrentChange" isPagin :curHeight="tableHeight" />
        </div>
      </div>
    </div>
    <Detail ref="detailRef" :info="detailInfo" :row="detailRowInfo" @refresh="getPage"/>
  </el-card>
</template>

<script>
const defaultQueryParams = {
  pageNum: 1,
  pageSize: 10,
};
import { getDanger, delDanger, addDanger, updateDanger,getDangerTask } from "@/api/risk/danger";
import Detail from './components/detail.vue'
export default {
  name: "Confirm",
  components:{ Detail },
  data() {
    return {
      tableHeight: 0,
      dialogFormVisible: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      action: process.env.VUE_APP_BASE_API + "/common/upload",
      tableData: [],
      // 查询参数
      queryParams: {
        ...defaultQueryParams,
        name: null,
        factoryId: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        name: [
          { required: true, message: "隐患名称不能为空", trigger: "blur" }
        ],
        desc: [
          { required: true, message: "隐患描述不能为空", trigger: "blur" }
        ],
        shopId: [
          { required: true, message: "车间不能为空", trigger: "change" }
        ],
        shopRoomId: [
          { required: true, message: "工房不能为空", trigger: "change" }
        ],
        imgUrl: [
          { required: true, message: "隐患图片不能为空",trigger: ["blur", "change"] }
        ],
      },
      tableColumns: [
        { key: "name", title: "隐患名称" },
        { key: "desc", title: "隐患描述" },
        { key: "shopName", title: "车间" },
        { key: "shopRoomName", title: "工房" },
        { key: "status", title: "隐患状态", render: (h, params) => {
          const statusOptions={
            '1': '已上报',
            '2': '已确认',
            '3': '已整改',
            '4': '已验收'
          };
          return h("div", [
            h("el-tag", {
              props: {
                  type: "success"
              },
            },
            statusOptions[params.row.status]),
          ]);
        }},
        {
          title: "操作",
          align: "center",
          fixed: 'right',
          width: 200,
          render: (h, params) => {
            return h("div", [
              h(
                "el-button",
                {
                  props: {
                    type: "text",
                    size: "small",
                  },
                  directives: [
                    {
                      name: "hasPermi",
                      value: ["risk:danger:edit"],
                    },
                  ],
                  on: {
                    click: () => {
                      this.handleUpdate(params.row, 1);
                    },
                  },
                },
                this.btnTitle
              ),
              h(
                "el-button",
                {
                  props: {
                    type: "text",
                    size: "small",
                  },
                  directives: [
                    {
                      name: "hasPermi",
                      value: ["risk:danger:info"],
                    },
                  ],
                  on: {
                    click: () => {
                      this.handleUpdate(params.row, 2)
                    },
                  },
                },
                "查看"
              ),
              h(
                "el-button",
                {
                  props: {
                    type: "text",
                    size: "small",
                  },
                  directives: [
                    {
                      name: "hasPermi",
                      value: ["risk:danger:info"],
                    },
                  ],
                  on: {
                    click: () => {
                      this.handleUpdate(params.row, 3)
                    },
                  },
                },
                "流程"
              ),
            ]);
          },
        },
      ],
      detailInfo:{},
      detailRowInfo:{},
      btnTitle:'',
      // options:[
      //   {route:'Confirm',title:"隐患确认",btnName:'确认',status:[{dictValue:'',dictName:'已确认'}]},
      //   {route:'Manage',title:"隐患治理与整改",btnName:'整改',status:[{dictValue:'未确认',dictName:'已确认'}]},
      //   {route:'CheckAccept',title:"隐患验收与评估",btnName:'验收',status:[{dictValue:'未确认',dictName:'已确认'}]},
      // ],
      btnOptions:{
        'Confirm':'确认',
        'Manage':'整改',
        'CheckAccept':'验收',
      },
      titleOptions:{
        'Confirm':'隐患确认',
        'Manage':'隐患治理与整改',
        'CheckAccept':'隐患验收与评估',
      },
    };
  },
  created() {
    this.getPage();
    const { name } = this.$route;
    this.btnTitle=this.btnOptions[name];
    this.title=this.titleOptions[name];
  },
  mounted() {
    this.$nextTick(() => {
      this.getTableHeight();
    })
  },
  methods: {
    getTableHeight() {
      let height = document.getElementsByClassName('ct-table-only')[0].clientHeight;
      this.tableHeight = height - 40;
    },
    handleCurrentChange(val) {
      this.queryParams.pageNum = val;
      this.getPage();
    },
    /** 查询隐患上报列表 */
    getPage() {
      this.loading = true;
      const { name } = this.$route;
      console.log('this.$route',this.$route);
      let statusOptions={
        'Confirm':1,
        'Manage':2,
        'CheckAccept':3,
      };
      let params={
          ...this.queryParams,
          status:statusOptions[name],
      };
      getDangerTask(params).then(response => {
        this.tableData = response.data.records; 
        this.total = response.data.total;
        this.loading = false;
      })
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        shopId: null,
        shopRoomId: null,
        desc: null,
        imgUrl: null,
        status: "0",
        isDanger: null,
        level: null,
        rectifyType: null,
        rectifyTime: null,
        rectifyAsk: null,
        factoryId: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        createDept: null,
        delFlag: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getPage();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 修改按钮操作 */
    handleUpdate(row, type) {
      this.detailRowInfo=row; 
      this.$refs.detailRef.open=true;
      this.$refs.detailRef.init(row,type);
      this.reset();
      const id = row.id || this.ids
      getDanger(id).then(response => {
        this.form = response.data;
        this.detailInfo = response.data;
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.form.imgUrl = JSON.stringify(this.fileList);
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateDanger(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.$refs.upload.$refs.uploadRef.clearFiles();
              this.open = false;
              this.getPage();
            });
          } else {
            addDanger(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.$refs.upload.$refs.uploadRef.clearFiles();
              this.open = false;
              this.getPage();
            });
          }
        }
      });
    },
  }
};
</script>
<style lang="scss" scoped>
  ::v-deep .el-dialog .el-dialog__body{
    margin-bottom:10px !important;
  }
</style>